using System.Text.Json.Serialization;

namespace McpApi.Models
{
    /// <summary>
    /// MCP协议基础模型定义
    /// </summary>

    #region JSON-RPC 基础模型

    /// <summary>
    /// JSON-RPC 2.0 请求基类
    /// </summary>
    public class JsonRpcRequest
    {
        [JsonPropertyName("jsonrpc")]
        public string JsonRpc { get; set; } = "2.0";

        [JsonPropertyName("id")]
        public object? Id { get; set; }

        [JsonPropertyName("method")]
        public string Method { get; set; } = string.Empty;

        [JsonPropertyName("params")]
        public object? Params { get; set; }
    }

    /// <summary>
    /// JSON-RPC 2.0 响应基类
    /// </summary>
    public class JsonRpcResponse
    {
        [JsonPropertyName("jsonrpc")]
        public string JsonRpc { get; set; } = "2.0";

        [JsonPropertyName("id")]
        public object? Id { get; set; }

        [JsonPropertyName("result")]
        public object? Result { get; set; }

        [JsonPropertyName("error")]
        public JsonRpcError? Error { get; set; }
    }

    /// <summary>
    /// JSON-RPC 错误信息
    /// </summary>
    public class JsonRpcError
    {
        [JsonPropertyName("code")]
        public int Code { get; set; }

        [JsonPropertyName("message")]
        public string Message { get; set; } = string.Empty;

        [JsonPropertyName("data")]
        public object? Data { get; set; }
    }

    #endregion

    #region MCP 协议模型

    /// <summary>
    /// MCP 初始化请求
    /// </summary>
    public class InitializeRequest
    {
        [JsonPropertyName("protocolVersion")]
        public string ProtocolVersion { get; set; } = "2024-11-05";

        [JsonPropertyName("capabilities")]
        public ClientCapabilities Capabilities { get; set; } = new();

        [JsonPropertyName("clientInfo")]
        public ClientInfo ClientInfo { get; set; } = new();
    }

    /// <summary>
    /// 客户端能力声明
    /// </summary>
    public class ClientCapabilities
    {
        [JsonPropertyName("tools")]
        public ToolsCapability? Tools { get; set; }
    }

    /// <summary>
    /// 工具能力
    /// </summary>
    public class ToolsCapability
    {
        [JsonPropertyName("listChanged")]
        public bool ListChanged { get; set; } = false;
    }

    /// <summary>
    /// 客户端信息
    /// </summary>
    public class ClientInfo
    {
        [JsonPropertyName("name")]
        public string Name { get; set; } = string.Empty;

        [JsonPropertyName("version")]
        public string Version { get; set; } = string.Empty;
    }

    /// <summary>
    /// MCP 初始化响应
    /// </summary>
    public class InitializeResponse
    {
        [JsonPropertyName("protocolVersion")]
        public string ProtocolVersion { get; set; } = "2024-11-05";

        [JsonPropertyName("capabilities")]
        public ServerCapabilities Capabilities { get; set; } = new();

        [JsonPropertyName("serverInfo")]
        public ServerInfo ServerInfo { get; set; } = new();
    }

    /// <summary>
    /// 服务器能力声明
    /// </summary>
    public class ServerCapabilities
    {
        [JsonPropertyName("tools")]
        public ToolsCapability? Tools { get; set; }
    }

    /// <summary>
    /// 服务器信息
    /// </summary>
    public class ServerInfo
    {
        [JsonPropertyName("name")]
        public string Name { get; set; } = string.Empty;

        [JsonPropertyName("version")]
        public string Version { get; set; } = string.Empty;
    }

    #endregion

    #region 工具相关模型

    /// <summary>
    /// 工具列表请求
    /// </summary>
    public class ListToolsRequest
    {
        [JsonPropertyName("cursor")]
        public string? Cursor { get; set; }
    }

    /// <summary>
    /// 工具列表响应
    /// </summary>
    public class ListToolsResponse
    {
        [JsonPropertyName("tools")]
        public List<Tool> Tools { get; set; } = new();

        [JsonPropertyName("nextCursor")]
        public string? NextCursor { get; set; }
    }

    /// <summary>
    /// 工具定义
    /// </summary>
    public class Tool
    {
        [JsonPropertyName("name")]
        public string Name { get; set; } = string.Empty;

        [JsonPropertyName("description")]
        public string Description { get; set; } = string.Empty;

        [JsonPropertyName("inputSchema")]
        public ToolInputSchema InputSchema { get; set; } = new();
    }

    /// <summary>
    /// 工具输入参数Schema
    /// </summary>
    public class ToolInputSchema
    {
        [JsonPropertyName("type")]
        public string Type { get; set; } = "object";

        [JsonPropertyName("properties")]
        public Dictionary<string, PropertySchema> Properties { get; set; } = new();

        [JsonPropertyName("required")]
        public List<string> Required { get; set; } = new();
    }

    /// <summary>
    /// 属性Schema定义
    /// </summary>
    public class PropertySchema
    {
        [JsonPropertyName("type")]
        public string Type { get; set; } = string.Empty;

        [JsonPropertyName("description")]
        public string Description { get; set; } = string.Empty;

        [JsonPropertyName("enum")]
        public List<string>? Enum { get; set; }
    }

    /// <summary>
    /// 工具调用请求
    /// </summary>
    public class CallToolRequest
    {
        [JsonPropertyName("name")]
        public string Name { get; set; } = string.Empty;

        [JsonPropertyName("arguments")]
        public Dictionary<string, object> Arguments { get; set; } = new();
    }

    /// <summary>
    /// 工具调用响应
    /// </summary>
    public class CallToolResponse
    {
        [JsonPropertyName("content")]
        public List<ToolContent> Content { get; set; } = new();

        [JsonPropertyName("isError")]
        public bool IsError { get; set; } = false;
    }

    /// <summary>
    /// 工具返回内容
    /// </summary>
    public class ToolContent
    {
        [JsonPropertyName("type")]
        public string Type { get; set; } = "text";

        [JsonPropertyName("text")]
        public string Text { get; set; } = string.Empty;
    }

    #endregion

    #region MES业务模型

    /// <summary>
    /// MES工具执行结果
    /// </summary>
    public class MESToolResult
    {
        public bool Success { get; set; }
        public string Message { get; set; } = string.Empty;
        public object? Data { get; set; }
        public string? ErrorCode { get; set; }
        public DateTime ExecutedAt { get; set; } = DateTime.UtcNow;
    }

    #endregion
}
